package com.jackiechancinema.ssm.service.Impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jackiechancinema.ssm.bo.DreamBo;
import com.jackiechancinema.ssm.config.SystemContant;
import com.jackiechancinema.ssm.dao.DreamDao;
import com.jackiechancinema.ssm.entity.Dreams;
import com.jackiechancinema.ssm.entity.Movices;
import com.jackiechancinema.ssm.service.Inte.DreamService;
import com.jackiechancinema.ssm.vo.PageVo;
import com.jackiechancinema.ssm.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class DreamServiceImpl implements DreamService {

    @Autowired
    private DreamDao dreamDao;

    @Override
    public PageInfo<Dreams> queryAll(int page, int limit) {
        PageHelper.startPage(page, limit);
        PageInfo<Dreams> pageInfo = new PageInfo<Dreams>(dreamDao.queryAll());
        return pageInfo;
    }

    @Override
    public PageVo<Dreams> findAllV2(DreamBo bo) {

        if (bo != null){
            //组装查询条件
            Dreams dreams = new Dreams();
            if (StringUtils.hasLength(bo.getDream_name())){
                dreams.setDream_name(bo.getDream_name());
            }
            if (StringUtils.hasLength(bo.getDream_principal())){
                dreams.setDream_principal(bo.getDream_principal());
            }

            PageHelper.startPage(bo.getPage(),bo.getLimit());
            PageInfo<Dreams> pageInfo = new PageInfo<>(dreamDao.queryAllV2(dreams));
            return new PageVo<Dreams>(SystemContant.PAGE_R,"OK",pageInfo.getTotal(),pageInfo.getList());

        }
        return new PageVo<Dreams>(SystemContant.PAGE_R,"OK",0 ,null);

    }

    @Override
    public R insertAdd(Dreams dreams) {
        int i = dreamDao.insertAdd(dreams);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R updateUpdate(Dreams dreams) {
        int i = dreamDao.updateUpdate(dreams);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

    @Override
    public R deleteDel(int id) {
        int i = dreamDao.deleteDel(id);
        if (i > 0){
            return R.ok(null);
        }else {
            return R.fail();
        }
    }

}
